package com.zdp.degisn.strategy_model.factory;

import com.zdp.degisn.strategy_model.strategy.car.CarFunction;
import com.zdp.degisn.strategy_model.strategy.car.SportCar;
import com.zdp.degisn.strategy_model.strategy.car.VanCar;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhengdp
 * @Description 汽车租赁工厂
 * @Date 2022/9/14 22:32
 * @Created by zhengdp
 */
public class CarFactory {


    private CarFactory(){}

    /**
     * 提前加载好所有类型的汽车，因为判断是取决于汽车自身
     */
    private final static List<CarFunction> cars = new ArrayList<>();
    static {
        // 这边就不使用SPI了
        cars.add(new SportCar());
        cars.add(new VanCar());
    }

    public static CarFunction getCar(String type){
        return cars.stream().filter(car -> car.support(type)).findFirst().orElseThrow(()->new RuntimeException("工厂中没有对应类型的车辆"));
    }

}
